'''
Desc: 植物大战僵尸主文件
Author: 陈爱国
Date: 2024-08-23 12:02:32
'''
import time
import pygame
from FlagZombie import FlagZombie
from Peashooter import Peashooter
from SunFlower import SunFlower
from WallNut import WallNut
from Sun import Sun
from Zombie import Zombie
from Bullet import Bullet

# 游戏初始化
pygame.init()

# 初始化音乐模块
pygame.mixer.init()
# 加载音乐
pygame.mixer.music.load("material/music/02 - Crazy Dave (Intro Theme).mp3")

# 设置游戏界面标题
pygame.display.set_caption("植物大战僵尸")

# 获取游戏界面
WIDTH = 1200
HEIGHT = 600
bg_size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# 帧率
FPS = 20

# 选择的植物
choose_plant = 0

# clock对象
clock = pygame.time.Clock()

# 用户自定义事件 - 产生太阳事件
GEN_SUN_EVENT = pygame.USEREVENT + 1
# 2s产生一个用户定义事件
pygame.time.set_timer(GEN_SUN_EVENT, 2000)

# 用户自定义事件 - 产生子弹事件
GEN_BULLET_EVENT = pygame.USEREVENT + 2
# 3s产生一个用户定义事件
pygame.time.set_timer(GEN_BULLET_EVENT, 1000)

# 用户自定义事件 - 产生僵尸事件
GEN_ZOMBIE_EVENT = pygame.USEREVENT + 3
# 3s产生一个用户定义事件
pygame.time.set_timer(GEN_ZOMBIE_EVENT, 5000)

# 用户自定义事件 - 产生旗帜僵尸事件
GEN_FLAGZOMBIE_EVENT = pygame.USEREVENT + 4
# 3s产生一个用户定义事件
pygame.time.set_timer(GEN_FLAGZOMBIE_EVENT, 8000)

# 背景
img_bg = pygame.image.load('material/images/background1.jpg').convert_alpha()
# 太阳花鼠标跟随图片
img_sunflower = pygame.image.load('material/images/SunFlower_00.png').convert_alpha()
# 坚果鼠标跟随图片
img_wallnut = pygame.image.load('material/images/WallNut_00.png').convert_alpha()
# 豌豆射手鼠标跟随图片
img_peashooter = pygame.image.load('material/images/Peashooter_00.png').convert_alpha()
# 计分牌
# img_sunback = pygame.image.load('material/images/SunBack.png').convert_alpha()
# 顶部卡片
img_seedbank = pygame.image.load('material/images/SeedBank.png').convert_alpha()
# 太阳花图片
img_sunflower_seed = pygame.image.load('material/images/TwinSunflower.gif').convert_alpha()
# 坚果
img_wallnut_seed = pygame.image.load('material/images/WallNut.gif').convert_alpha()
# 豌豆射手
img_peashooter_seed = pygame.image.load('material/images/Peashooter.gif').convert_alpha()
# 得分
text_score = 900
# 得分显示的字体
font_score = pygame.font.SysFont("arial", 20)
# 得分图像
img_score = font_score.render(str(text_score), True, (0, 0, 0))
# 创建豌豆射手对象
# peashooter = Peashooter()
# 创建太阳花对象
# sunflower = SunFlower()
# 创建坚果对象
# wallnut = WallNut()
# # 太阳数组
# sunlist = []
# 创建僵尸对象
# zombie = Zombie()

# 创建游戏精灵组
# spriteGroup = pygame.sprite.Group()
# 创建子弹精灵组
bulletSpriteGroup = pygame.sprite.Group()
# 创建僵尸精灵组
zombieSpriteGroup = pygame.sprite.Group()
# 创建太阳精灵组
sunSpriteGroup = pygame.sprite.Group()
# 创建坚果精灵组
wallNutSpriteGroup = pygame.sprite.Group()
# 创建豌豆射手精灵组
peashooterSpriteGroup = pygame.sprite.Group()
# 创建太阳花精灵组
sunFlowerSpriteGroup = pygame.sprite.Group()

# 将对象添加到游戏精灵组中
# spriteGroup.add(peashooter)
# spriteGroup.add(sunflower)
# spriteGroup.add(wallnut)
# spriteGroup.add(zombie)

def main():
    # 游戏状态
    running = True

    # 图片序列的索引
    index = 0

    while running:
        # 使用全局变量
        global text_score
        global img_score
        global choose_plant

        # 3s产生一个子弹
        # if index % 60 == 0:
        #     # 3s产生一个子弹
        #     bullet = Bullet(peashooter.rect, bg_size)
        #     spriteGroup.add(bullet)


        # 使用帧率
        clock.tick(FPS)

        # 播放音乐
        if not pygame.mixer.music.get_busy():
            pygame.mixer.music.play()

        # 僵尸和子弹的碰撞检测
        for zombie in zombieSpriteGroup:
            for bullet in bulletSpriteGroup:
                if pygame.sprite.collide_mask(bullet, zombie):
                    # 移除子弹
                    bulletSpriteGroup.remove(bullet)
                    # 僵尸能量-1
                    zombie.energy -= 1

        # 僵尸和坚果的碰撞检测
        for zombie in zombieSpriteGroup:
            for wallNut in wallNutSpriteGroup:
                if pygame.sprite.collide_mask(wallNut, zombie):
                    # 将僵尸添加到集合中
                    wallNut.zombies.add(zombie)
                    # 僵尸碰到了坚果
                    zombie.isMeetWallNut = True
                    # 如果僵尸死了，坚果能量不减少
                    if not zombie.isAlive:
                        continue
                    # 坚果能量-1
                    wallNut.energy -= 1
        
        # 僵尸和豌豆射手的碰撞检测
        for zombie in zombieSpriteGroup:
            for peashooter in peashooterSpriteGroup:
                if pygame.sprite.collide_mask(peashooter, zombie):
                    # 将僵尸添加到集合中
                    peashooter.zombies.add(zombie)
                    # 僵尸碰到了植物
                    zombie.isMeetWallNut = True
                    # 如果僵尸死了，坚果能量不减少
                    if not zombie.isAlive:
                        continue
                    # 豌豆射手能量-1
                    peashooter.energy -= 1

        # 僵尸和太阳花的碰撞检测
        for zombie in zombieSpriteGroup:
            for sunFlower in sunFlowerSpriteGroup:
                if pygame.sprite.collide_mask(sunFlower, zombie):
                    # 将僵尸添加到集合中
                    sunFlower.zombies.add(zombie)
                    # 僵尸碰到了植物
                    zombie.isMeetWallNut = True
                    # 如果僵尸死了，坚果能量不减少
                    if not zombie.isAlive:
                        continue
                    # 太阳花能量-1
                    sunFlower.energy -= 1

        # 监听退出事件
        for event in pygame.event.get():
            # 用户自定义事件 - 产生旗帜僵尸事件
            if event.type == GEN_FLAGZOMBIE_EVENT:
                flagzombie = FlagZombie()
                zombieSpriteGroup.add(flagzombie)
            # 用户自定义事件 - 产生僵尸事件
            if event.type == GEN_ZOMBIE_EVENT:
                zombie = Zombie()
                zombieSpriteGroup.add(zombie)
            # 用户自定义事件 - 产生太阳事件
            if event.type == GEN_SUN_EVENT:
                # # 2m在太阳花的周围产生一个太阳
                # sun = Sun(sunflower.rect)
                # # 将太阳添加到游戏精灵组中
                # sunSpriteGroup.add(sun)
                # pass
                for sunFlower in sunFlowerSpriteGroup:
                    # 当前时间
                    now = time.time()
                    # 太阳花生成时间大于等于5s，则生成太阳
                    if now - sunFlower.lasttime >= 5:
                        sun = Sun(sunFlower.rect)
                        sunSpriteGroup.add(sun)
                        sunFlower.lasttime = now
            elif event.type == GEN_BULLET_EVENT:
                # 3s在豌豆射手位置产生子弹
                for sprite in peashooterSpriteGroup:
                    bullet = Bullet(sprite.rect, bg_size)
                    bulletSpriteGroup.add(bullet)


            # 退出事件
            if event.type == pygame.QUIT:
                running = False

            # 判断鼠标是否按下
            if event.type == pygame.MOUSEBUTTONDOWN:
                # print(pygame.mouse.get_pressed())
                # 鼠标左键被按下
                if pygame.mouse.get_pressed()[0]:
                    # 获得鼠标点击时的坐标
                    pos = pygame.mouse.get_pos()
                    # print(pygame.mouse.get_pos())
                    # 判断鼠标是否点击了顶部卡片
                    (x, y) = pos
                    if 330<=x<=380 and 10<=y<=80 and text_score>=150:
                        # 太阳花
                        print('点击了太阳花')
                        choose_plant = 1
                        pass
                    elif 380<x<=430 and 10<=y<=80 and text_score>=50:
                        # 坚果
                        print('点击了坚果')
                        choose_plant = 2
                        pass
                    elif 430<x<480 and 10<=y<=80 and text_score>=100:
                        # 豌豆射手
                        print('点击了豌豆射手')
                        choose_plant = 3
                        pass
                    elif 250<=x<=1200 and 80<y<=600:
                        # 可以种植的位置
                        print('点击了可种植位置')
                        if choose_plant == 1:
                            # 种植太阳花
                            current_time = time.time()  # 太阳花生成时的时间
                            sunflower = SunFlower(current_time)
                            sunflower.rect.top = y
                            sunflower.rect.left = x
                            sunFlowerSpriteGroup.add(sunflower)

                            # 扣除相应分数
                            text_score -= 150
                            img_score = font_score.render(str(text_score), True, (0, 0, 0))

                            # 返回未选中状态
                            choose_plant  = 0
                        elif choose_plant == 2:
                            # 种植坚果
                            wallNut = WallNut()
                            wallNut.rect.top = y
                            wallNut.rect.left = x
                            wallNutSpriteGroup.add(wallNut)

                            # 扣除相应分数
                            text_score -= 50
                            img_score = font_score.render(str(text_score), True, (0, 0, 0))

                            # 返回未选中状态
                            choose_plant  = 0
                        elif choose_plant == 3:
                            # 种植豌豆射手
                            peashooter = Peashooter()
                            peashooter.rect.top = y
                            peashooter.rect.left = x
                            peashooterSpriteGroup.add(peashooter)

                            # 扣除相应分数
                            text_score -= 100
                            img_score = font_score.render(str(text_score), True, (0, 0, 0))

                            # 返回未选中状态
                            choose_plant  = 0

                    else:
                        pass 

                    for sun in sunSpriteGroup:
                        # 判断坐标是否在对象区域内
                        if sun.rect.collidepoint(pos):
                            # 在对象区域内，则移除太阳
                            sunSpriteGroup.remove(sun)
                            # 计数牌更新
                            text_score += 50
                            # 更新得分图像
                            img_score = font_score.render(str(text_score), True, (0, 0, 0))

        # 绘制背景
        screen.blit(img_bg, (0, 0))
        # 绘制计分牌
        # screen.blit(img_sunback, (250,0))
        # 绘制顶部卡片
        screen.blit(img_seedbank, (250, 0))
        # 绘制太阳花图片
        screen.blit(img_sunflower_seed, (330, 10))
        # 坚果
        screen.blit(img_wallnut_seed, (380, 10))
        # 豌豆射手
        screen.blit(img_peashooter_seed, (430, 10))
        # 绘制得分
        screen.blit(img_score, (265, 60))

        # 如果choose_plant = 1绘制伴随太阳花图片
        (x, y) = pygame.mouse.get_pos()
        if choose_plant == 1:
            # 绘制太阳花鼠标跟随
            screen.blit(img_sunflower, (x, y))
        elif choose_plant == 2:
            # 绘制坚果鼠标跟随
            screen.blit(img_wallnut, (x, y))
        elif choose_plant == 3:
            # 绘制豌豆射手鼠标跟随
            screen.blit(img_peashooter, (x, y))

        # 更新游戏精灵组
        # spriteGroup.update(index)
        # 绘制游戏精灵组
        # spriteGroup.draw(screen)

        # 更新僵尸精灵组
        zombieSpriteGroup.update(index)
        # 绘制僵尸精灵组
        zombieSpriteGroup.draw(screen)

        # 更新子弹精灵组
        bulletSpriteGroup.update(index)
        # 绘制子弹精灵组
        bulletSpriteGroup.draw(screen)

        # 更新太阳精灵组
        sunSpriteGroup.update(index)
        # 绘制太阳精灵组
        sunSpriteGroup.draw(screen)

        # 更新坚果精灵组
        wallNutSpriteGroup.update(index)
        # 绘制坚果精灵组
        wallNutSpriteGroup.draw(screen)

        # 更新豌豆射手精灵组
        peashooterSpriteGroup.update(index)
        # 绘制豌豆射手精灵组
        peashooterSpriteGroup.draw(screen)

        # 更新太阳花精灵组
        sunFlowerSpriteGroup.update(index)
        # 绘制太阳花精灵组
        sunFlowerSpriteGroup.draw(screen)

        # 图片索引+1
        index += 1

        # 翻转
        pygame.display.flip()


if __name__ == '__main__':
    main()